<template>
  <div class="cart">
    <div class="container">
      <div class="cart-header-section">
        <h1>购物车</h1>
        <div v-if="cartItems.length > 0" class="cart-progress">
          <div class="progress-step active">
            <div class="step-number">1</div>
            <div class="step-label">选择商品</div>
          </div>
          <div class="progress-line"></div>
          <div class="progress-step">
            <div class="step-number">2</div>
            <div class="step-label">确认订单</div>
          </div>
          <div class="progress-line"></div>
          <div class="progress-step">
            <div class="step-number">3</div>
            <div class="step-label">支付完成</div>
          </div>
        </div>
      </div>
      <div v-if="cartItems.length === 0" class="empty-cart">
        <div class="empty-icon">🛒</div>
        <p>购物车为空</p>
        <div class="empty-actions">
          <router-link to="/events" class="btn btn-primary">去选购演出票</router-link>
        </div>
      </div>
      <div v-else class="cart-content">
        <div class="cart-items">
          <!-- 购物车统计信息 -->
          <div class="cart-stats">
            <div class="stat-item">
              <div class="stat-icon">🎭</div>
              <div class="stat-content">
                <div class="stat-number">{{ cartItems.length }}</div>
                <div class="stat-label">演出项目</div>
              </div>
            </div>
            <div class="stat-item">
              <div class="stat-icon">🎫</div>
              <div class="stat-content">
                <div class="stat-number">{{ totalTickets }}</div>
                <div class="stat-label">门票数量</div>
              </div>
            </div>
            <div class="stat-item">
              <div class="stat-icon">💰</div>
              <div class="stat-content">
                <div class="stat-number">¥{{ subtotal }}</div>
                <div class="stat-label">商品总额</div>
              </div>
            </div>
            <div v-if="appliedCoupon" class="stat-item highlight">
              <div class="stat-icon">🎉</div>
              <div class="stat-content">
                <div class="stat-number">¥{{ appliedCoupon.discount }}</div>
                <div class="stat-label">优惠金额</div>
              </div>
            </div>
          </div>

          <div class="cart-header">
            <h2>购物清单 ({{ cartItems.length }} 项)</h2>
            <div class="cart-header-actions">
              <label class="select-all">
                <input type="checkbox" v-model="allSelected" />
                全选
              </label>
              <button @click="removeSelected" class="btn btn-danger" :disabled="selectedIds.length === 0">移除已选</button>
              <button @click="clearCart" class="btn btn-outline">清空购物车</button>
            </div>
          </div>
          
          <div v-for="item in cartItems" :key="item.id" class="cart-item">
            <div class="item-select">
              <input type="checkbox" v-model="selectedIds" :value="item.id" class="item-checkbox" />
            </div>
            
            <div class="item-content">
              <div class="item-main">
                <div class="item-image">
                  <img :src="resolveImage(item.posterUrl || item.image)" :alt="item.eventTitle" />
                  <div class="event-badge">演出票</div>
                </div>
                
                <div class="item-details">
                  <div class="item-header">
                    <h3 class="event-title">{{ item.eventTitle }}</h3>
                    <div class="item-actions">
                      <button 
                        @click="removeItem(item.id)" 
                        class="btn-remove" 
                        title="删除商品"
                        @mouseenter="showRemoveTooltip = item.id"
                        @mouseleave="showRemoveTooltip = null"
                      >
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                          <path d="M3 6h18M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2m3 0v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6h14zM10 11v6M14 11v6"/>
                        </svg>
                        <div v-if="showRemoveTooltip === item.id" class="tooltip">
                          删除《{{ item.eventTitle }}》
                        </div>
                      </button>
                    </div>
                  </div>
                  
                  <div class="event-meta">
                    <div class="meta-item">
                      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"/>
                        <circle cx="12" cy="10" r="3"/>
                      </svg>
                      <span>{{ item.venue }}</span>
                    </div>
                    <div class="meta-item">
                      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <rect x="3" y="4" width="18" height="18" rx="2" ry="2"/>
                        <line x1="16" y1="2" x2="16" y2="6"/>
                        <line x1="8" y1="2" x2="8" y2="6"/>
                        <line x1="3" y1="10" x2="21" y2="10"/>
                      </svg>
                      <span>{{ formatDate(item.date) }}</span>
                    </div>
                  </div>
                  
                  <div class="ticket-details">
                    <div class="ticket-type">
                      <span class="type-label">票种:</span>
                      <span class="type-value">{{ item.ticketType }}</span>
                    </div>
                    <div v-if="item.seatArea" class="seat-area">
                      <span class="area-label">区域:</span>
                      <span class="area-value">{{ item.seatArea }}</span>
                    </div>
                  </div>
                  
                  <div v-if="item.seats && item.seats.length > 0" class="seat-info">
                    <span class="seat-label">座位:</span>
                    <div class="seat-list">
                      <span v-for="seat in item.seats" :key="seat" class="seat-tag">{{ seat }}</span>
                    </div>
                  </div>
                </div>
              </div>
              
              <div class="item-controls">
                <div class="quantity-section">
                  <label class="quantity-label">数量</label>
                  <div class="quantity-controls">
                    <button 
                      @click="decreaseQuantity(item)" 
                      class="quantity-btn" 
                      :disabled="item.quantity <= 1 || operatingItemId === item.id"
                      :class="{ 'loading': operatingItemId === item.id }"
                    >
                      <svg v-if="operatingItemId !== item.id" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <line x1="5" y1="12" x2="19" y2="12"/>
                      </svg>
                      <div v-else class="loading-spinner"></div>
                    </button>
                    <span class="quantity-display" :class="{ 'updating': operatingItemId === item.id }">
                      {{ item.quantity }}
                    </span>
                    <button 
                      @click="increaseQuantity(item)" 
                      class="quantity-btn" 
                      :disabled="item.quantity >= 10 || operatingItemId === item.id"
                      :class="{ 'loading': operatingItemId === item.id }"
                    >
                      <svg v-if="operatingItemId !== item.id" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <line x1="12" y1="5" x2="12" y2="19"/>
                        <line x1="5" y1="12" x2="19" y2="12"/>
                      </svg>
                      <div v-else class="loading-spinner"></div>
                    </button>
                  </div>
                </div>
                
                <div class="price-section">
                  <div class="unit-price">
                    <span class="price-label">单价</span>
                    <span class="price-value">¥{{ item.price }}</span>
                  </div>
                  <div class="total-price">
                    <span class="total-label">小计</span>
                    <span class="total-value">¥{{ item.total }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        
        <div class="cart-sidebar">
          <!-- 优惠券区域 -->
          <div class="coupon-section">
            <h3>优惠券</h3>
            <div class="coupon-input">
              <input 
                v-model="couponCode" 
                type="text" 
                placeholder="请输入优惠券代码"
                class="coupon-field"
              />
              <button @click="applyCoupon" class="btn btn-secondary">使用</button>
            </div>
            <div v-if="appliedCoupon" class="applied-coupon">
              <div class="coupon-info">
                <span class="coupon-name">{{ appliedCoupon.name }}</span>
                <span class="coupon-discount">-¥{{ appliedCoupon.discount }}</span>
              </div>
              <button @click="removeCoupon" class="remove-coupon">×</button>
            </div>
            <div class="available-coupons">
              <p>可用优惠券:</p>
              <div 
                v-for="coupon in availableCoupons" 
                :key="coupon.code"
                class="coupon-item"
                @click="selectCoupon(coupon)"
              >
                <div class="coupon-name">{{ coupon.name }}</div>
                <div class="coupon-desc">{{ coupon.description }}</div>
              </div>
            </div>
          </div>
          
          <!-- 价格汇总 -->
          <div class="cart-summary">
            <h3>订单汇总</h3>
            <div class="summary-item">
              <span>商品总额:</span>
              <span>¥{{ subtotal }}</span>
            </div>
            <div class="summary-item" v-if="appliedCoupon">
              <span>优惠券折扣:</span>
              <span class="discount">-¥{{ appliedCoupon.discount }}</span>
            </div>
            <div class="summary-item" v-if="appliedCoupon">
              <span>已为您节省:</span>
              <span class="discount">¥{{ savedAmount }}</span>
            </div>
            <div class="summary-item">
              <span>服务费:</span>
              <span>¥{{ serviceFee }}</span>
            </div>
            <div class="summary-divider"></div>
            <div class="summary-total">
              <span>应付总额:</span>
              <span class="total">¥{{ finalTotal }}</span>
            </div>
            <div class="checkout-actions">
              <button 
                class="btn btn-primary btn-large" 
                @click="checkout"
                :disabled="isCheckingOut || cartItems.length === 0"
                :class="{ 'loading': isCheckingOut }"
              >
                <span v-if="!isCheckingOut">立即结算 ({{ cartItems.length }} 项)</span>
                <span v-else class="checkout-loading">
                  <div class="loading-spinner"></div>
                  正在准备结算...
                </span>
              </button>
              <button 
                class="btn btn-outline btn-large" 
                @click="continueShopping"
                :disabled="isCheckingOut"
              >
                继续购物
              </button>
            </div>
          </div>
          
          <!-- 购买须知 -->
          <div class="purchase-notice">
            <h4>购买须知</h4>
            <ul>
              <li>演出票一经售出，不退不换</li>
              <li>请提前30分钟到场，凭票入场</li>
              <li>1.2米以下儿童谢绝入场</li>
              <li>演出期间请保持安静，禁止拍照录像</li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useCartStore } from '../stores/cart'
import { useToastStore } from '@/stores/toast'
import { useRouter } from 'vue-router'
import { BASE_URL } from '@/api/client'
import FallbackPoster from '@/assets/images/phantom-opera.svg'

const cartStore = useCartStore()
const router = useRouter()
const toast = useToastStore()

// 状态管理
const isLoading = ref(false)
const isCheckingOut = ref(false)
const operatingItemId = ref(null)
const showRemoveTooltip = ref(null)

// 优惠券相关
const couponCode = ref('')
const appliedCoupon = computed(() => cartStore.appliedCoupon)

// 可用优惠券
const availableCoupons = ref([
  {
    code: 'WELCOME10',
    name: '新用户优惠券',
    description: '满100减10元',
    discount: 10,
    minAmount: 100
  },
  {
    code: 'SAVE20',
    name: '满减优惠券',
    description: '满200减20元',
    discount: 20,
    minAmount: 200
  },
  {
    code: 'VIP50',
    name: 'VIP专享券',
    description: '满500减50元',
    discount: 50,
    minAmount: 500
  }
])

const cartItems = computed(() => cartStore.items)
const totalTickets = computed(() => cartItems.value.reduce((total, item) => total + item.quantity, 0))

// 计算价格（使用store getters）
const subtotal = computed(() => cartStore.subtotal)
const serviceFee = computed(() => cartStore.serviceFee)
const couponDiscount = computed(() => cartStore.couponDiscount)
const finalTotal = computed(() => cartStore.finalTotal)
const savedAmount = computed(() => cartStore.couponDiscount)

// 批量选择逻辑
const selectedIds = ref([])
const allSelected = computed({
  get() {
    return cartItems.value.length > 0 && selectedIds.value.length === cartItems.value.length
  },
  set(val) {
    selectedIds.value = val ? cartItems.value.map(i => i.id) : []
  }
})

// 统一解析图片：后端上传路径补全 BASE_URL；缺失使用占位图
const resolveImage = (src) => {
  if (!src) return FallbackPoster
  if (typeof src === 'string' && src.startsWith('/uploads/')) return `${BASE_URL}${src}`
  return src
}

// 获取演出图片
const getEventImage = (eventId) => {
  const imageMap = {
    1: '/src/assets/images/swan-lake.svg',
    2: '/src/assets/images/phantom-opera.svg',
    3: '/src/assets/images/wulong-count.svg',
    4: '/src/assets/images/jay-concert.svg'
  }
  return imageMap[eventId] || '/src/assets/images/swan-lake.svg'
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  const weekday = weekdays[date.getDay()]
  return `${year}-${month}-${day} ${weekday}`
}



// 删除商品
const removeItem = (itemId) => {
  const item = cartItems.value.find(i => i.id === itemId)
  if (!item) return
  
  if (confirm(`确定要删除《${item.eventTitle}》吗？`)) {
    cartStore.removeItem(itemId)
    toast.success('商品已从购物车移除', {
      title: '移除成功',
      message: `《${item.eventTitle}》已从购物车移除`
    })
  }
}

// 批量移除
const removeSelected = () => {
  if (selectedIds.value.length === 0) {
    toast.warning('请先选择要删除的商品')
    return
  }
  
  if (confirm(`确定删除已选中的 ${selectedIds.value.length} 个商品吗？`)) {
    cartStore.removeItems(selectedIds.value)
    toast.success(`已删除 ${selectedIds.value.length} 个商品`, {
      title: '批量删除成功'
    })
    selectedIds.value = []
  }
}

// 清空购物车
const clearCart = () => {
  if (cartItems.value.length === 0) {
    toast.info('购物车已经是空的了')
    return
  }
  
  if (confirm('确定要清空购物车吗？此操作不可恢复。')) {
    const itemCount = cartItems.value.length
    cartStore.clearCart()
    toast.success(`已清空购物车`, {
      title: '清空成功',
      message: `已移除 ${itemCount} 个商品`
    })
  }
}

// 增加数量时的引导
const increaseQuantity = async (item) => {
  if (operatingItemId.value === item.id) return // 防止重复操作
  
  if (item.quantity >= 10) {
    toast.warning('每个商品最多只能购买10张票', {
      title: '数量限制',
      message: '如需购买更多，请联系客服'
    })
    return
  }
  
  operatingItemId.value = item.id
  
  try {
    cartStore.updateQuantity(item.id, item.quantity + 1)
    // 简短的成功提示，不打扰用户
    setTimeout(() => {
      if (operatingItemId.value === item.id) {
        toast.info(`数量已调整为 ${item.quantity + 1}`, {
          duration: 1500
        })
      }
    }, 200)
  } finally {
    setTimeout(() => {
      operatingItemId.value = null
    }, 300)
  }
}

// 减少数量时的引导
const decreaseQuantity = async (item) => {
  if (operatingItemId.value === item.id) return // 防止重复操作
  
  if (item.quantity <= 1) {
    toast.warning('数量不能少于1张', {
      title: '数量限制',
      message: '如要删除商品，请点击删除按钮'
    })
    return
  }
  
  operatingItemId.value = item.id
  
  try {
    cartStore.updateQuantity(item.id, item.quantity - 1)
    // 简短的成功提示，不打扰用户
    setTimeout(() => {
      if (operatingItemId.value === item.id) {
        toast.info(`数量已调整为 ${item.quantity - 1}`, {
          duration: 1500
        })
      }
    }, 200)
  } finally {
    setTimeout(() => {
      operatingItemId.value = null
    }, 300)
  }
}

// 应用优惠券
const applyCoupon = () => {
  if (!couponCode.value.trim()) {
    toast.warning('请输入优惠券代码')
    return
  }
  
  const coupon = availableCoupons.value.find(c => c.code === couponCode.value.toUpperCase())
  
  if (!coupon) {
    toast.error('优惠券代码无效', {
      title: '优惠券错误',
      message: '请检查优惠券代码是否正确'
    })
    return
  }
  
  if (appliedCoupon.value && appliedCoupon.value.code === coupon.code) {
    toast.info('此优惠券已经应用过了')
    return
  }
  
  if (subtotal.value < coupon.minAmount) {
    toast.warning(`此优惠券需要满${coupon.minAmount}元才能使用`, {
      title: '使用条件不满足',
      message: `当前商品总额：¥${subtotal.value}，还差¥${coupon.minAmount - subtotal.value}`
    })
    return
  }
  
  cartStore.applyCoupon(coupon)
  couponCode.value = ''
  toast.success(`优惠券应用成功！`, {
    title: '优惠券已生效',
    message: `${coupon.name} - 已为您节省¥${coupon.discount}`
  })
}

// 选择优惠券
const selectCoupon = (coupon) => {
  if (appliedCoupon.value && appliedCoupon.value.code === coupon.code) {
    toast.info('此优惠券已经应用过了')
    return
  }
  
  if (subtotal.value < coupon.minAmount) {
    toast.warning(`此优惠券需要满${coupon.minAmount}元才能使用`, {
      title: '使用条件不满足',
      message: `当前商品总额：¥${subtotal.value}，还差¥${coupon.minAmount - subtotal.value}`
    })
    return
  }
  
  cartStore.applyCoupon(coupon)
  toast.success(`优惠券应用成功！`, {
    title: '优惠券已生效',
    message: `${coupon.name} - 已为您节省¥${coupon.discount}`
  })
}

// 移除优惠券
const removeCoupon = () => {
  if (!appliedCoupon.value) {
    toast.info('当前没有应用优惠券')
    return
  }
  
  const couponName = appliedCoupon.value.name
  cartStore.removeCoupon()
  toast.info(`已移除优惠券`, {
    title: '优惠券已移除',
    message: `${couponName} 已移除`
  })
}

// 继续购物
const continueShopping = () => {
  toast.info('为您推荐更多精彩演出', {
    title: '继续购物',
    message: '发现更多精彩演出，享受文化盛宴'
  })
  router.push('/events')
}


// 结算前的检查和引导
const checkout = async () => {
  if (isCheckingOut.value) return // 防止重复点击
  
  if (cartItems.value.length === 0) {
    toast.warning('购物车为空', {
      title: '无法结算',
      message: '请先添加演出票到购物车'
    })
    return
  }
  
  isCheckingOut.value = true
  
  try {
    // 检查是否有即将开始的演出
    const now = new Date()
    const soonEvents = cartItems.value.filter(item => {
      const eventDate = new Date(item.date)
      const timeDiff = eventDate.getTime() - now.getTime()
      const hoursDiff = timeDiff / (1000 * 3600)
      return hoursDiff < 24 && hoursDiff > 0
    })
    
    if (soonEvents.length > 0) {
      toast.warning('购物车中有即将开始的演出', {
        title: '温馨提醒',
        message: '请确认演出时间，提前安排行程',
        duration: 5000
      })
      
      // 给用户时间查看提醒
      await new Promise(resolve => setTimeout(resolve, 2000))
    }
    
    // 显示结算提示
    toast.info('正在为您准备订单确认页面...', {
      title: '开始结算',
      message: `共 ${cartItems.value.length} 项商品，总计 ¥${finalTotal.value}`,
      duration: 2000
    })
    
    // 模拟加载延迟，增强用户体验
    await new Promise(resolve => setTimeout(resolve, 1500))
    
    // 跳转到订单确认页面
    router.push('/order-confirm')
    
  } catch (error) {
    toast.error('跳转结算页面失败', {
      title: '系统错误',
      message: '请稍后重试或联系客服'
    })
  } finally {
    isCheckingOut.value = false
  }
}
</script>

<style scoped>
.cart {
  min-height: 100vh;
  background-color: #fafafa;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px 16px;
}

.cart h1 {
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 32px;
  color: #212121;
}

/* 空购物车 */
.empty-cart {
  text-align: center;
  padding: 80px 0;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 16px;
}

.empty-cart p {
  font-size: 18px;
  color: #757575;
  margin-bottom: 24px;
}

.empty-actions {
  display: flex;
  gap: 1rem;
  justify-content: center;
  margin-top: 1.5rem;
}

.empty-cart .btn {
  margin-top: 0;
}

/* 购物车内容 */
.cart-content {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 32px;
}

/* 购物车商品列表 */
.cart-items {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.cart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 2px solid #f0f0f0;
}

.cart-header h2 {
  font-size: 20px;
  font-weight: bold;
  color: #212121;
}

.cart-header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.select-all {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: #757575;
}

/* 购物车商品项 */
.cart-item {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 24px;
  margin-bottom: 16px;
  background: white;
  border: 2px solid #f0f0f0;
  border-radius: 16px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.cart-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.cart-item:hover {
  border-color: #667eea;
  box-shadow: 0 8px 24px rgba(102, 126, 234, 0.15);
  transform: translateY(-2px);
}

.cart-item:hover::before {
  transform: scaleX(1);
}

.item-select {
  flex-shrink: 0;
  padding-top: 8px;
}

.item-checkbox {
  width: 18px;
  height: 18px;
  accent-color: #667eea;
  cursor: pointer;
}

.item-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.item-main {
  display: flex;
  gap: 20px;
}

.item-image {
  flex-shrink: 0;
  width: 120px;
  height: 160px;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.item-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.event-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 10px;
  font-weight: 600;
}

.item-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.item-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.event-title {
  font-size: 20px;
  font-weight: 700;
  color: #212529;
  margin: 0;
  line-height: 1.3;
}

.item-actions {
  display: flex;
  gap: 8px;
}

.btn-remove {
  width: 36px;
  height: 36px;
  border: 2px solid #e9ecef;
  background: white;
  color: #6c757d;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.btn-remove:hover {
  border-color: #dc3545;
  background: #dc3545;
  color: white;
  transform: scale(1.1);
}

.event-meta {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: #6c757d;
}

.meta-item svg {
  color: #667eea;
  flex-shrink: 0;
}

.ticket-details {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
}

.ticket-type,
.seat-area {
  display: flex;
  align-items: center;
  gap: 8px;
}

.type-label,
.area-label {
  font-size: 12px;
  color: #6c757d;
  font-weight: 500;
}

.type-value,
.area-value {
  font-size: 14px;
  color: #212529;
  font-weight: 600;
  padding: 4px 8px;
  background: #f8f9fa;
  border-radius: 6px;
}

.seat-info {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  flex-wrap: wrap;
}

.seat-label {
  font-size: 12px;
  color: #6c757d;
  font-weight: 500;
  padding-top: 6px;
}

.seat-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.seat-tag {
  padding: 4px 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.item-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 12px;
  border: 1px solid #e9ecef;
}

.quantity-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.quantity-label {
  font-size: 12px;
  color: #6c757d;
  font-weight: 500;
}

.quantity-controls {
  display: flex;
  align-items: center;
  gap: 12px;
}

.quantity-btn {
  width: 36px;
  height: 36px;
  border: 2px solid #667eea;
  background: white;
  color: #667eea;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.quantity-btn:hover:not(:disabled) {
  background: #667eea;
  color: white;
  transform: scale(1.1);
}

.quantity-btn:disabled {
  border-color: #e9ecef;
  color: #6c757d;
  cursor: not-allowed;
}

.quantity-btn:disabled:hover {
  background: white;
  transform: none;
}

.quantity-display {
  font-size: 18px;
  font-weight: 700;
  color: #212529;
  min-width: 40px;
  text-align: center;
  padding: 8px 12px;
  background: white;
  border: 2px solid #e9ecef;
  border-radius: 8px;
}

.price-section {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}

.unit-price,
.total-price {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.price-label,
.total-label {
  font-size: 12px;
  color: #6c757d;
  font-weight: 500;
}

.price-value {
  font-size: 16px;
  font-weight: 600;
  color: #212529;
}

.total-value {
  font-size: 20px;
  font-weight: 700;
  color: #e74c3c;
}

/* 侧边栏 */
.cart-sidebar {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* 优惠券区域 */
.coupon-section {
  background: white;
  border: 2px solid #f0f0f0;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.coupon-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
}

.coupon-section h3 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 20px;
  color: #212529;
  display: flex;
  align-items: center;
  gap: 8px;
}

.coupon-section h3::before {
  content: '🎫';
  font-size: 18px;
}

.coupon-input {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
}

.coupon-field {
  flex: 1;
  padding: 12px 16px;
  border: 2px solid #e9ecef;
  border-radius: 12px;
  font-size: 14px;
  transition: all 0.3s ease;
  background: #f8f9fa;
}

.coupon-field:focus {
  outline: none;
  border-color: #28a745;
  background: white;
  box-shadow: 0 0 0 3px rgba(40, 167, 69, 0.1);
}

.btn-apply {
  padding: 12px 20px;
  background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
  color: white;
  border: none;
  border-radius: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
}

.btn-apply:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(40, 167, 69, 0.3);
}

.applied-coupon {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%);
  border: 2px solid #28a745;
  padding: 16px;
  border-radius: 12px;
  margin-bottom: 20px;
  position: relative;
  overflow: hidden;
}

.applied-coupon::before {
  content: '✓';
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  background: #28a745;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
}

.coupon-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.coupon-name {
  font-weight: 700;
  color: #155724;
  font-size: 16px;
}

.coupon-discount {
  font-size: 14px;
  color: #28a745;
  font-weight: 600;
}

.remove-coupon {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #6c757d;
  padding: 4px;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.remove-coupon:hover {
  background: rgba(220, 53, 69, 0.1);
  color: #dc3545;
  transform: scale(1.1);
}

.available-coupons p {
  font-size: 14px;
  color: #6c757d;
  margin-bottom: 12px;
  font-weight: 500;
}

.coupon-item {
  padding: 12px 16px;
  border: 2px solid #e9ecef;
  border-radius: 12px;
  margin-bottom: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #f8f9fa;
}

.coupon-item:hover {
  border-color: #28a745;
  background: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(40, 167, 69, 0.15);
}

.coupon-item .coupon-name {
  font-weight: 600;
  font-size: 14px;
  color: #212529;
  margin-bottom: 4px;
}

.coupon-item .coupon-desc {
  font-size: 12px;
  color: #6c757d;
}

/* 订单汇总 */
.cart-summary {
  background: white;
  border: 2px solid #f0f0f0;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  position: relative;
  overflow: hidden;
}

.cart-summary::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.cart-summary h3 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 20px;
  color: #212529;
  display: flex;
  align-items: center;
  gap: 8px;
}

.cart-summary h3::before {
  content: '📋';
  font-size: 18px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  font-size: 15px;
  padding: 8px 0;
}

.summary-label {
  color: #6c757d;
  font-weight: 500;
}

.summary-value {
  color: #212529;
  font-weight: 600;
}

.discount {
  color: #28a745;
  font-weight: 700;
}

.summary-divider {
  height: 2px;
  background: linear-gradient(135deg, #e9ecef 0%, #dee2e6 100%);
  margin: 20px 0;
  border-radius: 1px;
}

.summary-total {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 24px;
  padding: 16px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 12px;
  border: 2px solid #dee2e6;
}

.total {
  color: #e74c3c;
  font-weight: 700;
}

.checkout-actions {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* 购买须知 */
.purchase-notice {
  background: white;
  border: 2px solid #f0f0f0;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  position: relative;
  overflow: hidden;
}

.purchase-notice::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(135deg, #ffc107 0%, #ff9800 100%);
}

.purchase-notice h4 {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 16px;
  color: #212529;
  display: flex;
  align-items: center;
  gap: 8px;
}

.purchase-notice h4::before {
  content: '⚠️';
  font-size: 16px;
}

.purchase-notice ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.purchase-notice li {
  font-size: 14px;
  color: #6c757d;
  margin-bottom: 12px;
  padding-left: 24px;
  position: relative;
  line-height: 1.5;
}

.purchase-notice li::before {
  content: '✓';
  position: absolute;
  left: 0;
  top: 2px;
  color: #28a745;
  font-weight: bold;
  width: 16px;
  height: 16px;
  background: rgba(40, 167, 69, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
}

/* 按钮样式 */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 24px;
  border-radius: 12px;
  text-decoration: none;
  font-weight: 600;
  transition: all 0.3s ease;
  border: none;
  cursor: pointer;
  font-size: 16px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.btn:hover::before {
  left: 100%;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(245, 87, 108, 0.3);
}

.btn-secondary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(245, 87, 108, 0.4);
}

.btn-danger {
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
  color: white;
  padding: 8px 16px;
  font-size: 14px;
  box-shadow: 0 2px 8px rgba(255, 107, 107, 0.3);
}

.btn-danger:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 107, 107, 0.4);
}

.btn-outline {
  background: white;
  color: #6c757d;
  border: 2px solid #e9ecef;
}

.btn-outline:hover {
  background: #f8f9fa;
  border-color: #667eea;
  color: #667eea;
  transform: translateY(-2px);
}

.btn-large {
  padding: 16px 32px;
  font-size: 18px;
  font-weight: 700;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .cart-content {
    grid-template-columns: 1fr;
    gap: 24px;
  }
  
  .cart-sidebar {
    order: -1;
  }
  
  .item-main {
    flex-direction: column;
    gap: 16px;
  }
  
  .item-image {
    width: 100%;
    height: 200px;
  }
}

@media (max-width: 768px) {
  .container {
    padding: 16px;
  }
  
  .page-header h1 {
    font-size: 24px;
  }
  
  .cart-header {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
  
  .cart-actions {
    justify-content: space-between;
  }
  
  .cart-item {
    padding: 16px;
    margin-bottom: 12px;
  }
  
  .item-content {
    gap: 16px;
  }
  
  .item-header {
    flex-direction: column;
    gap: 12px;
    align-items: stretch;
  }
  
  .event-title {
    font-size: 18px;
  }
  
  .item-image {
    height: 160px;
  }
  
  .ticket-details {
    flex-direction: column;
    gap: 12px;
  }
  
  .item-controls {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
  
  .quantity-section {
    align-items: stretch;
  }
  
  .quantity-controls {
    justify-content: center;
  }
  
  .price-section {
    align-items: center;
  }
  
  .coupon-section,
  .cart-summary,
  .purchase-notice {
    padding: 20px;
  }
  
  .coupon-input {
    flex-direction: column;
    gap: 12px;
  }
  
  .btn-apply {
    width: 100%;
  }
  
  .checkout-actions .btn {
    width: 100%;
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .container {
    padding: 12px;
  }
  
  .page-header h1 {
    font-size: 20px;
  }
  
  .cart-item {
    padding: 12px;
  }
  
  .item-image {
    height: 120px;
  }
  
  .event-title {
    font-size: 16px;
  }
  
  .meta-item {
    font-size: 12px;
  }
  
  .quantity-btn {
    width: 32px;
    height: 32px;
  }
  
  .quantity-display {
    font-size: 16px;
    padding: 6px 10px;
  }
  
  .price-value,
  .total-value {
    font-size: 16px;
  }
  
  .total-value {
    font-size: 18px;
  }
  
  .coupon-section,
  .cart-summary,
  .purchase-notice {
    padding: 16px;
  }
  
  .coupon-section h3,
  .cart-summary h3,
  .purchase-notice h4 {
    font-size: 16px;
  }
  
  .summary-total {
    font-size: 18px;
    padding: 12px;
  }
  
  .btn {
    padding: 12px 20px;
    font-size: 14px;
  }
  
  .btn-large {
    padding: 14px 24px;
    font-size: 16px;
  }
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.cart-item {
  animation: fadeIn 0.5s ease-out;
}

.cart-sidebar > * {
  animation: slideIn 0.5s ease-out;
}

.cart-sidebar > *:nth-child(2) {
  animation-delay: 0.1s;
}

.cart-sidebar > *:nth-child(3) {
  animation-delay: 0.2s;
}

/* 加载状态和动画 */
.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid #e2e8f0;
  border-top: 2px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  display: inline-block;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 数量控制按钮加载状态 */
.quantity-btn.loading {
  opacity: 0.7;
  cursor: not-allowed;
  background-color: #f8fafc;
  border-color: #e2e8f0;
}

.quantity-btn.loading:hover {
  background-color: #f8fafc;
  border-color: #e2e8f0;
  transform: none;
}

/* 数量显示更新状态 */
.quantity-display.updating {
  color: #667eea;
  font-weight: 600;
  transform: scale(1.05);
  transition: all 0.3s ease;
}

/* 结算按钮加载状态 */
.btn.loading {
  position: relative;
  color: transparent;
  cursor: not-allowed;
  opacity: 0.8;
}

.checkout-loading {
  display: flex;
  align-items: center;
  gap: 8px;
  color: white;
  font-weight: 500;
}

.checkout-loading .loading-spinner {
  border-color: rgba(255, 255, 255, 0.3);
  border-top-color: white;
}

/* 按钮禁用状态增强 */
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.btn:disabled:hover {
  background-color: initial;
  border-color: initial;
  transform: none;
}

/* 购物车头部区域 */
.cart-header-section {
  margin-bottom: 2rem;
}

.cart-header-section h1 {
  margin-bottom: 1.5rem;
}

/* 进度指示器 */
.cart-progress {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 1.5rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  color: white;
  margin-bottom: 1rem;
}

.progress-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  opacity: 0.6;
  transition: all 0.3s ease;
}

.progress-step.active {
  opacity: 1;
  transform: scale(1.1);
}

.step-number {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 14px;
  transition: all 0.3s ease;
}

.progress-step.active .step-number {
  background: white;
  color: #667eea;
  box-shadow: 0 4px 12px rgba(255, 255, 255, 0.3);
}

.step-label {
  font-size: 12px;
  font-weight: 500;
  text-align: center;
}

.progress-line {
  width: 60px;
  height: 2px;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 1px;
}

/* 购物车统计信息 */
.cart-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 2rem;
  padding: 1.5rem;
  background: #f8fafc;
  border-radius: 12px;
  border: 1px solid #e2e8f0;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem;
  background: white;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
  transition: all 0.3s ease;
  cursor: default;
}

.stat-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border-color: #667eea;
}

.stat-item.highlight {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-color: transparent;
}

.stat-item.highlight:hover {
  transform: translateY(-2px) scale(1.02);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
}

.stat-icon {
  font-size: 24px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f1f5f9;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.stat-item.highlight .stat-icon {
  background: rgba(255, 255, 255, 0.2);
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 18px;
  font-weight: 700;
  color: #1e293b;
  margin-bottom: 2px;
}

.stat-item.highlight .stat-number {
  color: white;
}

.stat-label {
  font-size: 12px;
  color: #64748b;
  font-weight: 500;
}

.stat-item.highlight .stat-label {
  color: rgba(255, 255, 255, 0.9);
}

/* 工具提示 */
.tooltip {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: white;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 12px;
  white-space: nowrap;
  z-index: 1000;
  margin-bottom: 8px;
  opacity: 0;
  animation: tooltipFadeIn 0.2s ease forwards;
}

.tooltip::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 4px solid transparent;
  border-top-color: #1e293b;
}

@keyframes tooltipFadeIn {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(-4px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* 删除按钮增强 */
.btn-remove {
  position: relative;
}

.btn-remove:hover {
  background-color: #fee2e2;
  border-color: #fecaca;
  color: #dc2626;
  transform: scale(1.1);
}

/* 操作反馈动画 */
.cart-item {
  transition: all 0.3s ease;
}

.cart-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

/* 成功状态动画 */
@keyframes success-pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

.success-animation {
  animation: success-pulse 0.6s ease-out;
}

/* 错误状态动画 */
@keyframes error-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

.error-animation {
  animation: error-shake 0.5s ease-out;
}

/* 优惠券应用成功动画 */
.coupon-success {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  animation: success-pulse 0.6s ease-out;
}

/* 购物车项目移除动画 */
@keyframes item-remove {
  0% {
    opacity: 1;
    transform: translateX(0);
    max-height: 200px;
  }
  50% {
    opacity: 0;
    transform: translateX(-100px);
    max-height: 200px;
  }
  100% {
    opacity: 0;
    transform: translateX(-100px);
    max-height: 0;
    padding: 0;
    margin: 0;
  }
}

.item-removing {
  animation: item-remove 0.5s ease-out forwards;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .cart-progress {
    flex-direction: column;
    gap: 1rem;
    padding: 1rem;
  }
  
  .progress-line {
    width: 2px;
    height: 30px;
  }
  
  .cart-stats {
    grid-template-columns: 1fr;
    gap: 0.75rem;
    padding: 1rem;
  }
  
  .stat-item {
    padding: 0.75rem;
  }
  
  .stat-icon {
    font-size: 20px;
    width: 32px;
    height: 32px;
  }
  
  .stat-number {
    font-size: 16px;
  }
}
</style>